package com.work.yshop.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsControllerCompat;

import com.work.yshop.R;
import com.work.yshop.databinding.ActivityRegisterBinding;
import com.work.yshop.mvp.contract.RegisterContract;
import com.work.yshop.mvp.presenter.RegisterPresenter;

/**
 * 注册页面Activity - MVP架构
 */
public class RegisterActivity extends AppCompatActivity implements RegisterContract.View {

    private ActivityRegisterBinding binding;
    private RegisterPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 设置状态栏
        setupStatusBar();
        
        binding = ActivityRegisterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 初始化Presenter
        presenter = new RegisterPresenter();
        presenter.attachView(this);

        // 初始化监听器
        setupListeners();
        // 设置用户协议可点击
        setupAgreementText();
    }
    
    /**
     * 设置状态栏
     */
    private void setupStatusBar() {
        // 让内容区域延伸到状态栏
        WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
        
        // 设置状态栏为浅色，图标为深色
        WindowInsetsControllerCompat controller = new WindowInsetsControllerCompat(getWindow(), getWindow().getDecorView());
        controller.setAppearanceLightStatusBars(true);
        
        // 设置状态栏透明
        getWindow().setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
    }

    /**
     * 设置各种监听器
     */
    private void setupListeners() {
        // 返回按钮
        binding.ivBack.setOnClickListener(v -> finish());

        // 获取验证码按钮点击事件
        binding.btnGetCode.setOnClickListener(v -> {
            String phone = binding.etPhone.getText().toString().trim();
            presenter.getVerificationCode(phone);
        });

        // 注册按钮点击事件
        binding.btnRegister.setOnClickListener(v -> {
            String phone = binding.etPhone.getText().toString().trim();
            String verificationCode = binding.etVerificationCode.getText().toString().trim();
            String password = binding.etPassword.getText().toString().trim();
            String confirmPassword = binding.etConfirmPassword.getText().toString().trim();
            boolean isAgreementChecked = binding.cbAgreement.isChecked();
            
            presenter.register(phone, verificationCode, password, confirmPassword, isAgreementChecked);
        });

        // 已有账号点击事件
        binding.tvLogin.setOnClickListener(v -> finish());
    }

    /**
     * 设置用户协议和隐私政策可点击
     */
    private void setupAgreementText() {
        SpannableString spannableString = new SpannableString("我已阅读并同意《用户协议》和《隐私政策》");
        
        // 用户协议可点击
        ClickableSpan userAgreementSpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                Toast.makeText(RegisterActivity.this, "跳转到用户协议页面", Toast.LENGTH_SHORT).show();
                // 实际项目中应该跳转到用户协议页面
                // Intent intent = new Intent(RegisterActivity.this, UserAgreementActivity.class);
                // startActivity(intent);
            }
        };
        
        // 隐私政策可点击
        ClickableSpan privacyPolicySpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                Toast.makeText(RegisterActivity.this, "跳转到隐私政策页面", Toast.LENGTH_SHORT).show();
                // 实际项目中应该跳转到隐私政策页面
                // Intent intent = new Intent(RegisterActivity.this, PrivacyPolicyActivity.class);
                // startActivity(intent);
            }
        };
        
        // 设置可点击范围
        spannableString.setSpan(userAgreementSpan, 7, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(privacyPolicySpan, 14, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        binding.tvAgreement.setText(spannableString);
        binding.tvAgreement.setMovementMethod(LinkMovementMethod.getInstance());
        binding.tvAgreement.setHighlightColor(ContextCompat.getColor(this, android.R.color.transparent));
    }

    @Override
    public void onVerificationCodeSent(String code) {
        // 自动填充验证码到输入框
        binding.etVerificationCode.setText(code);
        Toast.makeText(this, "验证码已发送: " + code, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void updateCountDownTimer(int seconds) {
        binding.btnGetCode.setEnabled(false);
        binding.btnGetCode.setText(seconds + "秒后重发");
    }

    @Override
    public void onCountDownFinished() {
        binding.btnGetCode.setEnabled(true);
        binding.btnGetCode.setText("获取验证码");
    }

    @Override
    public void showPhoneError(String message) {
        if (message == null) {
            binding.tilPhone.setError(null);
        } else {
            binding.tilPhone.setError(message);
        }
    }

    @Override
    public void showVerificationCodeError(String message) {
        if (message == null) {
            binding.tilVerificationCode.setError(null);
        } else {
            binding.tilVerificationCode.setError(message);
        }
    }

    @Override
    public void showPasswordError(String message) {
        if (message == null) {
            binding.tilPassword.setError(null);
        } else {
            binding.tilPassword.setError(message);
        }
    }

    @Override
    public void showConfirmPasswordError(String message) {
        if (message == null) {
            binding.tilConfirmPassword.setError(null);
        } else {
            binding.tilConfirmPassword.setError(message);
        }
    }

    @Override
    public void showLoading() {
        binding.btnRegister.setEnabled(false);
        binding.btnRegister.setText("注册中...");
    }

    @Override
    public void hideLoading() {
        binding.btnRegister.setEnabled(true);
        binding.btnRegister.setText(R.string.register);
    }

    @Override
    public void showError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRegisterSuccess() {
        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
        
        // 注册成功后直接进入主页
        Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
        binding = null;
    }
} 